package service4gateway

import (
	UserAction "MyBlogv2/Servers/UserAction/userAction"
	"MyBlogv2/Servers/UserAction/useractionservice"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/discov"
	"github.com/zeromicro/go-zero/zrpc"
	"strconv"
)

var clientOfUserAction UserAction.UserActionServiceClient

func init() {
	conn := zrpc.MustNewClient(zrpc.RpcClientConf{
		Etcd: discov.EtcdConf{
			Hosts: []string{"127.0.0.1:2379"},
			Key:   "useraction.rpc",
		},
	})
	clientOfUserAction = useractionservice.NewUserActionService(conn)

}
func Like(c *gin.Context) {
	typeOfLike, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")

	rsp, err := clientOfUserAction.Like(c.Request.Context(), &UserAction.UserActionRequest{
		Type:   int64(typeOfLike),
		TypeId: int64(typeId),
		UserId: int64(userId.(int)),
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	if rsp.Code == 200 {
		response.ResponseOK(c, nil)
		return
	} else {
		response.ResponseError(c, HttpCode.AppHttpCodeEnum(rsp.Code))
		return
	}
}

func CancelLike(c *gin.Context) {
	typeOfLike, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	_, err := clientOfUserAction.CancelLike(c.Request.Context(), &UserAction.UserActionRequest{
		Type:   int64(typeOfLike),
		TypeId: int64(typeId),
		UserId: int64(userId.(int)),
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	response.ResponseOK(c, nil)

}

func IsLike(c *gin.Context) {
	typeOfLike, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	rsp, err := clientOfUserAction.IsLike(c.Request.Context(), &UserAction.UserActionRequest{
		Type:   int64(typeOfLike),
		TypeId: int64(typeId),
		UserId: int64(userId.(int)),
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	if rsp.Code == 200 {
		var likeVoList []models.LikeVo
		copier.Copy(&likeVoList, &rsp.Data)
		for i, v := range rsp.Data {
			likeVoList[i].IdStr = v.Id
			likeVoList[i].CreateTime = helper.TimestampToMyTime(v.CreateTime)
			likeVoList[i].UpdateTime = helper.TimestampToMyTime(v.UpdateTime)
		}
		response.ResponseOK(c, rsp.Data)
		return
	} else {
		response.ResponseError(c, HttpCode.AppHttpCodeEnum(rsp.Code))
		return
	}
}

func Favorite(c *gin.Context) {
	typeOfFavorite, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	rsp, err := clientOfUserAction.Favorite(c.Request.Context(), &UserAction.UserActionRequest{
		Type:   int64(typeOfFavorite),
		TypeId: int64(typeId),
		UserId: int64(userId.(int)),
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	if rsp.Code == 200 {
		response.ResponseOK(c, nil)
		return
	} else {
		response.ResponseError(c, HttpCode.AppHttpCodeEnum(rsp.Code))
		return
	}

}

func CancelFavorite(c *gin.Context) {
	typeOfFavorite, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	rsp, _ := clientOfUserAction.CancelFavorite(c.Request.Context(), &UserAction.UserActionRequest{
		Type:   int64(typeOfFavorite),
		TypeId: int64(typeId),
		UserId: int64(userId.(int)),
	})
	response.CommonResponse(c, HttpCode.AppHttpCodeEnum(rsp.Code), rsp.Data, rsp.Msg)
	return
}

func IsFavorite(c *gin.Context) {
	typeOfFavorite, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	rsp, err := clientOfUserAction.IsFavorite(c.Request.Context(), &UserAction.UserActionRequest{
		Type:   int64(typeOfFavorite),
		TypeId: int64(typeId),
		UserId: int64(userId.(int)),
	})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	response.ResponseOK(c, rsp.Data)
}
